package mshop.com.schild_kotlin.fragments.search

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import kotlinx.android.synthetic.main.fragment_home.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.adapters.RankAdapter
import mshop.com.schild_kotlin.base.BaseFragment
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.entitys.SearchBean
import mshop.com.schild_kotlin.mvp.base.BaseContract
import mshop.com.schild_kotlin.mvp.presenter.SearchPresenter

/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */

class SearchFragment : BaseFragment(), BaseContract.View<SearchBean> {
    var searchAdapter: RankAdapter? = null
    var searchPresenter: SearchPresenter? = null
    var start: Int = 0
    var mList = ArrayList<ItemList>()
    var search_key: String = ""

    //静态方法
    companion object {
        fun getInstall(search_key: String): SearchFragment {
            val fragment = SearchFragment()
            val bundle = Bundle()
            bundle.putString("search_key", search_key)
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun initView() {
        search_key = arguments?.getString("search_key", "") ?: ""
        searchAdapter = RankAdapter(mList)
        recycle_view.layoutManager = LinearLayoutManager(context)
        recycle_view.adapter = searchAdapter
        searchAdapter?.notifyDataSetChanged()
        searchPresenter = context?.let { SearchPresenter(it, this) }

        //设置下拉刷新监听
        refreshLayout?.setOnRefreshListener(this)
        recycle_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val layoutManager: LinearLayoutManager = recyclerView?.layoutManager as LinearLayoutManager
                val lastPosition = layoutManager.findLastVisibleItemPosition()
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition >= mList.size - 3) {
                    if (!TextUtils.isEmpty(search_key)) {
                        searchPresenter?.searchKey(search_key, mList.size + 1)
                    }
                }
            }
        })
    }

    override fun getLayoutResources(): Int {
        return R.layout.fragment_home
    }

    override fun setData(bean: SearchBean) {

        if (refreshLayout!!.isRefreshing) {
            refreshLayout?.isRefreshing = false
            if (mList.size > 0) {
                mList.clear()
            }
        }


        bean.itemList
                .filter { it.type.equals("video") }
                .forEach { mList.add(it) }
        searchAdapter?.notifyDataSetChanged()
    }

    /**
     * Called when a swipe gesture triggers a refresh.
     */
    override fun onRefresh() {
        if (!TextUtils.isEmpty(search_key))
            searchPresenter?.searchKey(search_key, 1)
    }

    fun search(searchKey: String) {
        search_key = searchKey
        onRefresh()
    }

    override fun getNeedRefresh(): Boolean {
        return true
    }


}